﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MCChuoiKyTuAnh;

namespace VTGGoldMiner
{
    //lop duoc tinh toam theo chuan man hinh 640x480 hien thi co dan theo ti le.
    //dau vao la cac hinh anh chuan chua duoc co dan.
    class NhanVat
    {
        private DaoVang trochoi;
        private Texture2D[] anhnhanvat = new Texture2D[11];
        private Texture2D anhhienthi;
        private Texture2D daumoc1, daumoc2, htdaumoc;
        private CacHam dungham;
        public string ngonnguht = "english";
        private int chayhoatcanh = 0, gocxoay = 360;
        private string chieuquay = "trai", chieukeo = "xuong";
        private float tiledanrong, tiledandai, gockeo;
        public int toadoxnv = 270, toadoynv = 97;
        private int toadoxgoc, toadoygoc, toadoxxet, toadoyxet, toadodaumocx, toadodaumocy;
        public Rectangle vungmoc;//vung moc gap, kich thuoc 10,10, di chuyen theo diem xet
        private int dangthadaykeo = 0;
        private float tocdolonnhat = 7, tocdokeo;//toc do cao nhat la 7.
        private float dodaidaykeo = 500, dodaidaydatha = 13;
        private int giasomocx, giasomocy;
        public int daquyg = 0, daquyy = 0, daquyb = 0, daquyr = 0;//bo da quy
        public int dhoamayman = 0, dphao = 0, ddonghothoigian = 0, ddaykeothan = 0, dnuocruavang = 0, dnuocruakimcuong = 0, dsachsuutap = 0;
        public PhaoNo huphaono, huthuocno;
        public int thuocnophat = 0, tamthuocnox, tamthuocnoy, bankinhno = 100;
        public double diemso = 500;
        private VatThe vattheduockeo;
        private Texture2D anhbaogiatri = null;
        private int toadochubaox = 370, toadochubaoy = 100;
        private SoundEffect amphongday, amkeoday, amantien, ambosuutap;
        private MChuoiKyTuAnh kytuhienthi1, kytuhienthi2;
        private Boolean ttdichuyen = false, kieuchudiem = true;
        private int toadoxdich, tddichuyen = 3;
        public NhanVat(DaoVang trochoichinh, MChuoiKyTuAnh napbochu1, MChuoiKyTuAnh napbochu2, Texture2D[] anh, Texture2D[] daumoc, Texture2D[] anhno, double diem, int nhoamayman, int nphao, int ndongho, int ndaykeo, int nnuocvang, int nkimcuong, int nsach, string ngonngu, SoundEffect amphong, SoundEffect amkeo, SoundEffect aman, SoundEffect amsuutap, float tilerong, float tiledai)
        {
            trochoi = trochoichinh;
            dungham = new CacHam(trochoi.GraphicsDevice);
            tiledanrong = tilerong;
            tiledandai = tiledai;
            huphaono = new PhaoNo(trochoi, anhno, 2, 0, 0, 100, 100, tiledanrong, tiledandai);
            huthuocno = huphaono;
            kytuhienthi1 = napbochu1;
            kytuhienthi2 = napbochu2;
            anhnhanvat = anh;
            daumoc1 = daumoc[0];
            daumoc2 = daumoc[1];
            anhhienthi = anhnhanvat[0];
            htdaumoc = daumoc1;
            toadoxgoc = Convert.ToInt32(toadoxnv + 32);
            toadoygoc = Convert.ToInt32(toadoynv + 61);
            ToaDoDichChuyen();
            tocdokeo = tocdolonnhat;
            diemso = diem; dhoamayman = nhoamayman; dphao = nphao; ddonghothoigian = ndongho;
            ddaykeothan = ndaykeo; dnuocruavang = nnuocvang; dnuocruakimcuong = nkimcuong; dsachsuutap = nsach;
            ngonnguht = ngonngu;
            amkeoday = amkeo; amantien = aman; amphongday = amphong; ambosuutap = amsuutap;
        }
        public void HoatCanhKeo(float tocdo, float coam)
        {
            int tocdoht = 3;
            if (tocdo <= 1) tocdoht = 7;
            else if (tocdo == tocdolonnhat) tocdoht = 1;
            chayhoatcanh = chayhoatcanh + 1;
            if (chayhoatcanh == 1 * tocdoht) anhhienthi = anhnhanvat[1];
            else if (chayhoatcanh == 2 * tocdoht) anhhienthi = anhnhanvat[2];
            else if (chayhoatcanh == 3 * tocdoht) anhhienthi = anhnhanvat[3];
            else if (chayhoatcanh == 4 * tocdoht) anhhienthi = anhnhanvat[4];
            else if (chayhoatcanh == 5 * tocdoht) anhhienthi = anhnhanvat[5];
            else if (chayhoatcanh == 6 * tocdoht) anhhienthi = anhnhanvat[6];
            else if (chayhoatcanh == 7 * tocdoht) anhhienthi = anhnhanvat[7];
            else if (chayhoatcanh == 8 * tocdoht) anhhienthi = anhnhanvat[8];
            else if (chayhoatcanh == 9 * tocdoht) { anhhienthi = anhnhanvat[9]; chayhoatcanh = 0; amkeoday.Play(coam, 0, 0); }
        }
        public void DungTimKiem()
        {
            chayhoatcanh = 0;
            anhhienthi = anhnhanvat[0];
            if (chieuquay == "trai")
            {
                if (gocxoay < 450 - 3) gocxoay += 2;
                else chieuquay = "phai";
            }
            else if (chieuquay == "phai")
            {
                if (gocxoay > 270 + 3) gocxoay -= 2;
                else chieuquay = "trai";
            }
        }
        public void ThaDayKeo(MouseState chuothientai, MouseState chuottruocdo, ref int thoigian, int somanchoi, float chinham)
        {
            if (dangthadaykeo == 0)//van dang dong dua
            {
                DungTimKiem();
                if (chuothientai.LeftButton == ButtonState.Pressed && chuottruocdo.LeftButton == ButtonState.Released)
                {
                    if (chuothientai.Y > (toadoynv + 83) * tiledandai)
                    {
                        ttdichuyen = false;
                        amphongday.Play(chinham, 0, 0);
                        chayhoatcanh = 0;
                        anhhienthi = anhnhanvat[10];
                        gockeo = (630 - gocxoay) * (float)Math.PI / 180;
                        dangthadaykeo = 1;
                    }
                }
            }
            else if (dangthadaykeo == 1)//neu tha dau keo roi thi thuc hien
            {
                if (chieukeo == "xuong")
                {
                    if (dodaidaydatha < dodaidaykeo)
                    {
                        dodaidaydatha = dodaidaydatha + tocdolonnhat;
                        ToaDoDichChuyen();
                    }
                    else chieukeo = "len";
                }
                else if (chieukeo == "len")
                {
                    if (dodaidaydatha > 13)
                    {
                        if (vattheduockeo != null) vattheduockeo.DichChuyen(toadoxxet - giasomocx, toadoyxet - giasomocy);
                        HoatCanhKeo(tocdokeo, chinham);
                        dodaidaydatha = dodaidaydatha - tocdokeo;
                        ToaDoDichChuyen();
                    }
                    else
                    {
                        if (vattheduockeo != null)
                        {
                            if (vattheduockeo.theloai == "daquyb") daquyb++;
                            else if (vattheduockeo.theloai == "daquyg") daquyg++;
                            else if (vattheduockeo.theloai == "daquyr") daquyr++;
                            else if (vattheduockeo.theloai == "daquyy") daquyy++;
                            if ((daquyb != 0) & (daquyg != 0) & (daquyr != 0) & (daquyy != 0))
                            {
                                ambosuutap.Play(chinham, 0, 0);
                                vattheduockeo.giatri = vattheduockeo.giatri + vattheduockeo.giatri * (10 + somanchoi);
                                daquyb = 0;
                                daquyg = 0;
                                daquyr = 0;
                                daquyy = 0;
                            }
                            anhbaogiatri = kytuhienthi1.XuatAnhKyTu(vattheduockeo.giatri.ToString() + "$", 0, 0, 1, 1, 1, 1, Color.Transparent);
                            toadochubaox = 370;
                            kieuchudiem = true;
                            if (vattheduockeo.theloai == "tuimayman")
                            {
                                int giatringaunhien = Convert.ToInt32(trochoi.ngaunhien.Next(1, 10));//1=>9
                                if (giatringaunhien == 1)
                                {
                                    dhoamayman++;
                                    if (ngonnguht == "english")
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("LUCKY FLOWERS", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    else
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("HOA MAY MẮN", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    kieuchudiem = false;
                                }
                                else if (giatringaunhien == 2)
                                {
                                    thoigian += 15;
                                    if (thoigian > 99) thoigian = 99;
                                    if (ngonnguht == "english")
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("CLOCK TIME", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    else
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("Đ.HỒ THỜI GIAN", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    kieuchudiem = false;
                                }
                                else if (giatringaunhien == 3)
                                {
                                    if (dphao < 99) dphao++;
                                    if (ngonnguht == "english")
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("A FIRECRACKERS", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    else
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("1 QUẢ PHÁO", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    kieuchudiem = false;
                                }
                                else if (giatringaunhien == 4)
                                {
                                    ddaykeothan++;
                                    if (ngonnguht == "english")
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("ZIPPERS GOD", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    else
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("DÂY KÉO THẦN", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    kieuchudiem = false;
                                }
                                else if (giatringaunhien == 5)
                                {
                                    dnuocruavang++;
                                    if (ngonnguht == "english")
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("GOLD WASH", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    else
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("NƯỚC RỦA VÀNG", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    kieuchudiem = false;
                                }
                                else if (giatringaunhien == 6)
                                {
                                    dnuocruakimcuong++;
                                    if (ngonnguht == "english")
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("DIAMOND WASH", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    else
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("NƯỚC RỦA K.CƯƠNG", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    kieuchudiem = false;
                                }
                                else if (giatringaunhien == 7)
                                {
                                    dsachsuutap++;
                                    if (ngonnguht == "english")
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("BOOK COLLECTION", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    else
                                        anhbaogiatri = kytuhienthi2.XuatAnhKyTu("SÁCH SƯU TẬP", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                    kieuchudiem = false;
                                }
                                else
                                {
                                    vattheduockeo.giatri = Convert.ToInt32(trochoi.ngaunhien.Next(10, 100 * somanchoi));
                                    if (dhoamayman != 0) vattheduockeo.giatri += Convert.ToInt32((float)vattheduockeo.giatri * (float)somanchoi / 10f);
                                    anhbaogiatri = kytuhienthi1.XuatAnhKyTu(vattheduockeo.giatri.ToString() + "$", 0, 0, 1, 1, 1, 1, Color.Transparent);
                                }
                            }
                            if (vattheduockeo.theloai == "hombimat")
                            {
                                vattheduockeo.giatri = Convert.ToInt32(trochoi.ngaunhien.Next(100, 300 * somanchoi));
                                int tanggiam = Convert.ToInt32(trochoi.ngaunhien.Next(1, 4));//1=>3
                                if (tanggiam == 3) vattheduockeo.giatri = -vattheduockeo.giatri;
                                if (dhoamayman != 0) vattheduockeo.giatri += Math.Abs(Convert.ToInt32((float)vattheduockeo.giatri * (float)somanchoi / 10f));
                                anhbaogiatri = kytuhienthi1.XuatAnhKyTu(vattheduockeo.giatri.ToString() + "$", 0, 0, 1, 1, 1, 1, Color.Transparent);
                            }
                            amantien.Play(chinham, 0, 0);
                            tocdokeo = tocdolonnhat;
                            diemso = diemso + vattheduockeo.giatri;
                            if (diemso < 0) diemso = 0;
                            //dua vat the len vi tri tren
                            vattheduockeo.gocxoay = 0;
                            vattheduockeo.DichChuyen(toadoxnv + 50 + Convert.ToInt32(trochoi.ngaunhien.Next(0, 201)) - 100 - Convert.ToInt32(vattheduockeo.chieurong / 2), 180 - vattheduockeo.chieudai);
                            vattheduockeo.tinhtrang = 0;
                            vattheduockeo = null;
                        }
                        chieukeo = "xuong";
                        DauMocCap("truot");
                        dangthadaykeo = 0;
                        dodaidaydatha = 13;
                        ToaDoDichChuyen();
                    }
                }
            }
            if (anhbaogiatri != null)
            {
                if (toadochubaox < 390) toadochubaox++;
                else if (toadochubaox < 420) toadochubaox = toadochubaox + 3;
                else if (toadochubaox < 490) toadochubaox = toadochubaox + 5;
                else if (toadochubaox < 640) toadochubaox = toadochubaox + 7;
                else
                {
                    toadochubaox = 370;
                    anhbaogiatri = null;
                }
            }
            huphaono.ChayNoPhao(2, 1);
            huthuocno.ChayNoPhao(2, 1);
        }
        public void DauMocCap(string trungtruot)
        {
            if (trungtruot == "trung")
                htdaumoc = daumoc2;
            else if (trungtruot == "truot")
                htdaumoc = daumoc1;
        }
        public int DangDoTim()
        {
            int ketqua = 0;
            if ((dangthadaykeo == 1) & (chieukeo == "xuong")) ketqua = 1;
            return ketqua;//neu tha day keo xuong thi tra ve 1
        }
        public void KeoVatThe(VatThe vatthekeo, int somanchoi)
        {
            vattheduockeo = vatthekeo;
            if (vattheduockeo.theloai == "thuocno")
            {
                tamthuocnox = vattheduockeo.toadox + Convert.ToInt32(vattheduockeo.chieurong / 2);
                tamthuocnoy = vattheduockeo.toadoy + Convert.ToInt32(vattheduockeo.chieudai / 2);
                huthuocno.NapThongTinNo(300, 300, tamthuocnox, tamthuocnoy);
                huthuocno.baonophao = 1;
                thuocnophat = 1;
                vattheduockeo.anhien = 2;//lam mat vat the di
                vattheduockeo.tenvatthe = "rong";
                tocdokeo = tocdolonnhat;
            }
            else
            {
                vattheduockeo.tinhtrang = 1;
                vattheduockeo.tamxoay = new Vector2(vattheduockeo.diembigap.X, vattheduockeo.diembigap.Y);
                vattheduockeo.gocxoay = gocxoay;
                vattheduockeo.DichChuyen(toadoxxet - Convert.ToInt32(vattheduockeo.diembigap.X + vattheduockeo.toadox) + vattheduockeo.toadox, toadoyxet - Convert.ToInt32(vattheduockeo.diembigap.Y + vattheduockeo.toadoy) + vattheduockeo.toadoy);
                giasomocx = toadoxxet - vattheduockeo.toadox;
                giasomocy = toadoyxet - vattheduockeo.toadoy;

                if ((vattheduockeo.theloai == "tuimayman") | (vattheduockeo.theloai == "hombimat"))
                {
                    int kieunangtuimayman = Convert.ToInt32(trochoi.ngaunhien.Next(1, 6));//1=>5
                    if (kieunangtuimayman == 1) vattheduockeo.donang = 1f;
                    else if (kieunangtuimayman == 2) vattheduockeo.donang = 3f;
                    else if (kieunangtuimayman == 3) vattheduockeo.donang = 5f;
                    else if (kieunangtuimayman == 4) vattheduockeo.donang = 6f;
                    else if (kieunangtuimayman == 5) vattheduockeo.donang = 6.3f;
                }

                if ((dsachsuutap != 0) & ((vattheduockeo.theloai == "da") | (vattheduockeo.theloai == "xuong"))) vattheduockeo.giatri = vattheduockeo.giatri + Convert.ToInt32(vattheduockeo.giatri * (somanchoi + 100) / 100);
                if ((dnuocruavang != 0) & (vattheduockeo.theloai == "vang")) vattheduockeo.giatri = vattheduockeo.giatri + Convert.ToInt32(vattheduockeo.giatri * (somanchoi + 70) / 100);
                if ((dnuocruakimcuong != 0) & (vattheduockeo.theloai == "kimcuong")) vattheduockeo.giatri = vattheduockeo.giatri + Convert.ToInt32(vattheduockeo.giatri * (somanchoi + 50) / 100);

                if (vattheduockeo.donang >= tocdolonnhat) tocdokeo = 0.1f;
                else tocdokeo = tocdolonnhat - vattheduockeo.donang;
                if (ddaykeothan != 0) tocdokeo = tocdolonnhat;//neu trang bi day keo than thi max toc do keo
            }
            chieukeo = "len";
        }
        public void SuDungPhaoNo()
        {
            dphao--;
            huphaono.NapThongTinNo(200, 200, toadoxxet, toadoyxet);
            huphaono.baonophao = 1;
            if (vattheduockeo != null)
            {
                vattheduockeo.anhien = 2;
                vattheduockeo = null;
            }
            tocdokeo = tocdolonnhat;
        }
        public void TrangThaiDau()//trang thai moi lan thang hoac thua
        {
            dangthadaykeo = 0;
            chieukeo = "xuong";
            DauMocCap("truot");
            dodaidaydatha = 13;
            ToaDoDichChuyen();
            tocdokeo = tocdolonnhat;
            huphaono.LamMoiPhaoNo();
            huthuocno.LamMoiPhaoNo();
            thuocnophat = 0;
            vattheduockeo = null;
            toadochubaox = 370;
            anhbaogiatri = null;
            ttdichuyen = false;
            toadoxnv = 270;
            toadoxgoc = Convert.ToInt32(toadoxnv + 32);
            ToaDoDichChuyen();
            QuetVatPham();
        }
        public void QuetVatPham()
        {
            dhoamayman = 0;
            ddonghothoigian = 0;
            ddaykeothan = 0;
            dnuocruavang = 0;
            dnuocruakimcuong = 0;
            dsachsuutap = 0;
            daquyb = 0;
            daquyg = 0;
            daquyr = 0;
            daquyy = 0;
        }
        private void ToaDoDichChuyen()
        {
            toadoxxet = Convert.ToInt32(dodaidaydatha * Math.Cos(gockeo)) + toadoxgoc;
            toadoyxet = toadoygoc - Convert.ToInt32(dodaidaydatha * Math.Sin(gockeo));
            vungmoc = new Rectangle(toadoxxet - 5, toadoyxet - 5, 10, 10);
            toadodaumocx = Convert.ToInt32((dodaidaydatha - 13) * Math.Cos(gockeo)) + toadoxgoc - 17;
            toadodaumocy = toadoygoc - Convert.ToInt32((dodaidaydatha - 13) * Math.Sin(gockeo));
        }
        public void DiChuyen()
        {
            if (ttdichuyen)
            {
                if (toadoxdich > toadoxnv + 32)
                {
                    toadoxnv += tddichuyen;
                    if (toadoxnv + 32 >= toadoxdich)
                    {
                        toadoxnv = toadoxdich - 32;
                        ttdichuyen = false;
                    }
                    toadoxgoc = Convert.ToInt32(toadoxnv + 32);
                    ToaDoDichChuyen();
                }
                else if (toadoxdich < toadoxnv + 32)
                {
                    toadoxnv -= tddichuyen;
                    if (toadoxnv + 32 <= toadoxdich)
                    {
                        toadoxnv = toadoxdich - 32;
                        ttdichuyen = false;
                    }
                    toadoxgoc = Convert.ToInt32(toadoxnv + 32);
                    ToaDoDichChuyen();
                }
            }
        }
        public void ThietLapDiChuyen(MouseState chuothientai, MouseState chuottruocdo, float chinham)
        {
            if (dangthadaykeo == 0)
            {
                if (chuothientai.LeftButton == ButtonState.Pressed && chuottruocdo.LeftButton == ButtonState.Released)
                {
                    if ((chuothientai.Y > 83 * tiledandai) & (chuothientai.Y < 166 * tiledandai))
                    {
                        amkeoday.Play(chinham, 0, 0);
                        toadoxdich = Convert.ToInt32(chuothientai.X / tiledanrong);
                        ttdichuyen = true;
                    }
                }
            }
        }
        public void HienThi(SpriteBatch nenve)
        {
            nenve.Draw(anhhienthi, dungham.BoHinhAnh(toadoxnv, toadoynv, 99, 83, tiledanrong, tiledandai), Color.White);
            if (vattheduockeo != null) vattheduockeo.HienThi(nenve);
            Color mauday = Color.Black;
            if (ddaykeothan != 0) mauday = Color.Red;
            if (dodaidaydatha > 13) dungham.VeDuongThang(nenve, 3, mauday, new Vector2(toadoxgoc * tiledanrong, toadoygoc * tiledandai), new Vector2(toadoxxet * tiledanrong, toadoyxet * tiledandai));
            dungham.XoayHinhAnh(nenve, htdaumoc, gocxoay, dungham.BoHinhAnh(toadodaumocx, toadodaumocy, 33, 27, tiledanrong, tiledandai), new Vector2(17, 0), tiledanrong, tiledandai);
            huthuocno.HienThi(nenve);
            huphaono.HienThi(nenve);
            if (anhbaogiatri != null) nenve.Draw(anhbaogiatri, dungham.BoHinhAnh(toadochubaox, toadochubaoy, anhbaogiatri.Width, anhbaogiatri.Height, tiledanrong, tiledandai), Color.White);
        }
        public void PhucHoiChuoiKyTu()
        {
            if (kieuchudiem) anhbaogiatri = kytuhienthi1.PhucHoiHinhAnh();
            else anhbaogiatri = kytuhienthi2.PhucHoiHinhAnh();
        }
    }
}